package loveqq.crud.util;

import org.reactivestreams.Publisher;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.function.Supplier;

public class WebUtil {

    public static <T> Mono<ServerResponse> response(Publisher<T> publisher) {
        return publisher.getClass().toString().equals("class reactor.core.publisher.MonoIgnorePublisher")
                ? ServerResponse.ok().build() : ServerResponse.ok().body(publisher, new ParameterizedTypeReference<T>() {
        });
    }

    public static <T> Mono<ServerResponse> response(Supplier<Publisher<T>> supplier) {
        return response(supplier.get());
    }

}
